<template>
  <div>
      <div class="login-container"></div>
    <!-- NavBar 组件：只需提供 title 标题 -->
    <van-nav-bar :fixed = 'true' title="黑马头条 - 登录" />
<van-form  @submit="login">
  <van-field v-model="form.mobile" type="tel" label="手机号码"
   placeholder="请输入手机号码" required></van-field>
  <van-field v-model="form.code" type="password" label="登录密码"
   placeholder="请输入登录密码" required></van-field>
  <div style="margin: 16px;">
    <van-button round block type="info" native-type="submit">提交</van-button>
  </div>
</van-form>
  </div>
</template>

<script>
import {loginAPI} from '../../api/userAPI'
import {mapMutations} from 'vuex'
export default {
  name: 'Login',
  data() {
    return {
      form: {
        mobile: '',
        code: '',
      },
      rules: {
      // 手机号的校验规则
        mobile: [{ required: true, message: '请填写您的手机号', trigger: 'onBlur' }, { pattern: /^1\d{10}$/, message: '请填写正确的手机号', trigger: 'onBlur' }],
        // 密码的校验规则
        code: [{ required: true, message: '请填写您的密码', trigger: 'onBlur' }],
      },
    };
  },
  methods: {
    ...mapMutations(['updateTokenInfo']),
     async login() {
       const res= await loginAPI(this.form)
       console.log(res.data.message)
       if(res.data.message=='OK'){
         this.updateTokenInfo(res.data.data)
        //  this.$router.push('/') 
       }
    },
  },
};
</script>

<style lang="less" scoped>
.login-container {
  padding-top: 46px;
}
</style>
